package tech.aistar.prj.controller;

import com.alibaba.fastjson.JSONArray;
import tech.aistar.prj.entity.Pet;
import tech.aistar.prj.entity.Result;
import tech.aistar.prj.service.IPetService;
import tech.aistar.prj.service.impl.PetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 本类功能:
 *
 * @author cxylk
 * @date 2020/9/18 19:00
 */
@WebServlet(urlPatterns = "/json/index")
public class PetAction extends HttpServlet {
    IPetService petService=new PetServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置文档类型
        resp.setContentType("text/json;charset=utf-8");
        PrintWriter out=resp.getWriter();

        List<Pet> petList=petService.findAll();

        Result result=new Result("200","查询成功",petList);

        String str= JSONArray.toJSONString(result);

        out.print(str);
    }
}
